package com.pine.web.pineframework.controller;

import cn.hutool.core.lang.tree.Tree;
import com.pine.common.beans.ResultVo;
import com.pine.common.beans.pineframework.request.SysDeptQueryRequest;
import com.pine.common.beans.pineframework.request.SysDeptSaveOrUpdateRequest;
import com.pine.common.beans.pineframework.response.SysDeptQueryResponse;
import com.pine.common.beans.request.BatchIdRequest;
import com.pine.common.beans.request.IdRequest;
import com.pine.common.beans.response.DeleteResponse;
import com.pine.common.beans.response.DropDownTreeResponse;
import com.pine.common.systemlog.annotation.SysLog;
import com.pine.common.util.time.TimeUtil;
import com.pine.common.util.tree.TreeUtil;
import com.pine.dao.pineframework.entity.SysDept;
import com.pine.manager.pineframework.trigger.SysDeptTriggerManager;
import com.pine.service.pineframework.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统部门表 前端控制器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-16
 */
@Tag(name = "系统部门表前端控制器")
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {

    @Resource
    private SysDeptService sysDeptService;

    /**
     * 查询部门树形结构
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/listTree")
    @Operation(summary = "查询部门树形结构")
    @SysLog("查询部门树形结构")
    public ResultVo<List<Tree<Long>>> listTree(@RequestBody @Valid SysDeptQueryRequest request) {
        List<SysDeptQueryResponse> queryResponses = sysDeptService.list(request);

        // 转换为树形结构
        List<Tree<Long>> trees = TreeUtil.convertLongIdTree(queryResponses, (map, resp) -> {
            map.put("status", resp.getStatus());
            map.put("code", resp.getCode());
            map.put("createTime", TimeUtil.defaultFormat(resp.getCreateTime()));
            map.put("updateTime", TimeUtil.defaultFormat(resp.getUpdateTime()));
        });
        return ResultVo.success(trees);
    }

    /**
     * 下拉部门树形结构
     *
     * @return 响应
     */
    @PostMapping(value = "/dropDownTree")
    @Operation(summary = "下拉部门树形结构")
    @SysLog("下拉部门树形结构")
    public ResultVo<List<Tree<Long>>> dropDownTree() {
        // 查询所有启用中的部门信息
        List<DropDownTreeResponse<Long>> dropDownTreeResponses = sysDeptService.dropDownTree();

        // 转换为树形结构
        List<Tree<Long>> trees = TreeUtil.convertLongIdTree(dropDownTreeResponses, null);
        return ResultVo.success(trees);
    }


    /**
     * 删除
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/delete")
    @Operation(summary = "删除信息")
    @SysLog("删除信息")
    public ResultVo<DeleteResponse> delete(@RequestBody @Valid IdRequest<Long> request) {
        // 获取部门和子部门
        List<SysDept> sysDeptList = sysDeptService.listDeptAndChild(request.getId());
        List<Long> deptIds = sysDeptList.stream().map(SysDept::getId).distinct().toList();

        // 批量删除
        BatchIdRequest<Long> batchIdRequest = new BatchIdRequest<>();
        batchIdRequest.setIds(deptIds);
        return ResultVo.success(SysDeptTriggerManager.delete(batchIdRequest));
    }

    /**
     * 新增
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/save")
    @Operation(summary = "新增信息")
    @SysLog("新增信息")
    public ResultVo<Boolean> save(@RequestBody @Valid SysDeptSaveOrUpdateRequest request) {
        Boolean triggerResult = SysDeptTriggerManager.saveOrUpdate(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 更新
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/update")
    @Operation(summary = "更新信息")
    @SysLog("更新信息")
    public ResultVo<Boolean> update(@RequestBody @Valid SysDeptSaveOrUpdateRequest request) {
        Boolean triggerResult = SysDeptTriggerManager.saveOrUpdate(request);
        return ResultVo.success(triggerResult);
    }
}
